#include "hjcCommon.h"

namespace ui
{
	CCRect VisibleRect::s_visibleRect;

	void VisibleRect::lazyInit()
	{
		if (s_visibleRect.size.width == 0.0f && s_visibleRect.size.height == 0.0f)
		{
			CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
			s_visibleRect.origin = pEGLView->getVisibleOrigin();
			s_visibleRect.size = pEGLView->getVisibleSize();
		}
	}
	CCRect VisibleRect::getVisibleRect()
	{
		lazyInit();
		return CCRectMake(s_visibleRect.origin.x, s_visibleRect.origin.y, s_visibleRect.size.width, s_visibleRect.size.height);
	}
	CCPoint VisibleRect::left()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x, s_visibleRect.origin.y+s_visibleRect.size.height/2);
	}
	CCPoint VisibleRect::right()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y+s_visibleRect.size.height/2);
	}
	CCPoint VisibleRect::top()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y+s_visibleRect.size.height);
	}
	CCPoint VisibleRect::bottom()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y);
	}
	CCPoint VisibleRect::center()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x+s_visibleRect.size.width/2, s_visibleRect.origin.y+s_visibleRect.size.height/2);
	}
	CCPoint VisibleRect::leftTop()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x, s_visibleRect.origin.y+s_visibleRect.size.height);
	}
	CCPoint VisibleRect::rightTop()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y+s_visibleRect.size.height);
	}
	CCPoint VisibleRect::leftBottom()
	{
		lazyInit();
		return s_visibleRect.origin;
	}
	CCPoint VisibleRect::rightBottom()
	{
		lazyInit();
		return ccp(s_visibleRect.origin.x+s_visibleRect.size.width, s_visibleRect.origin.y);
	}


	CCSize getWindowRealSize()
	{
		return CCEGLView::sharedOpenGLView()->getFrameSize();
	}
	CCSize getDesignResolution()
	{
		return CCEGLView::sharedOpenGLView()->getDesignResolutionSize();
	}
	CCRect getVisibleRect()
	{
		return VisibleRect::getVisibleRect();
	}
	CCPoint getNodePosition(CCNode* pNode)
	{
		return pNode->getPosition();
	}
	void setNodePosition(CCNode* pNode, CCPoint pt)
	{
		pNode->setPosition(pt);
	}
	CCSize getNodeSize(CCNode* pNode)
	{
		return CCSize(pNode->getContentSize().width  * pNode->getScaleX(), pNode->getContentSize().height * pNode->getScaleY());
	}
	bool nodeContainsPoint(CCNode* pNode, CCPoint ptGL)
	{
		CCPoint pt = ui::getNodePosition(pNode);
		CCSize sz = ui::getNodeSize(pNode);
		CCRect rc(pt.x - sz.width/2, pt.y - sz.height/2, sz.width, sz.height);
		return rc.containsPoint(ptGL);
	}
	bool nodeOutOfScopeAbove(CCNode* pNode)
	{
		CCPoint pt = ui::getNodePosition(pNode);
		CCSize sz = ui::getNodeSize(pNode);
		CCRect rc = VisibleRect::getVisibleRect();
		return pt.y - sz.width / 2 > rc.origin.y + rc.size.height;
	}
	void placeUI(CCNode* pNode, float xPercent, float yPercent)
	{
		CCRect rc = VisibleRect::getVisibleRect();
		pNode->setPosition(rc.origin.x + rc.size.width * xPercent, rc.origin.y + rc.size.height * yPercent);
	}
};
